//
//  DomainViewController.swift
//  SmallParts
//
//  Created by 航汇聚 on 2019/12/13.
//  Copyright © 2019 BellaZhou. All rights reserved.
//

import UIKit

class DomainViewController: UIViewController {
    @IBOutlet weak var collectionView: UICollectionView!
    var domains: NSMutableArray?
    var selectedDomains: NSMutableArray = NSMutableArray(array: [DomainItemObject(_id: "0", name: "全部")])
    var isEdit: Bool = true {
        didSet {
            self.collectionView.reloadSections(NSIndexSet(index: 0) as IndexSet)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.requestNumInfo(id: "0")
    }
    @IBAction func touchAction(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }
}
extension DomainViewController {
    func requestNumInfo(id: String) {
        let upload = UploadingData.init()
        upload.uploadData("http://api.cqvip.com/qikanapp/GetDomain.ashx", "id=\(id)", .post)
        upload.success = { (data, urlString) -> () in
            let result = try? JSONDecoder().decode(DomainResult.self, from: data!)
            self.domains = NSMutableArray(array: result!.result)
            self.collectionView.reloadData()
        }
    }
}
extension DomainViewController: UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            return self.selectedDomains.count
        }else {
            return self.domains == nil ? 0 : self.domains!.count
        }
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "domainCollectionCell", for: indexPath) as! DomainCollectionCell
        if indexPath.section == 0 {
            let domianItem = self.selectedDomains[indexPath.item] as! DomainItemObject
            cell.domainLabel.text = domianItem.name
            cell.delegate = self
            if indexPath.item == 0 && !self.isEdit {
                //可编辑模式下,第一个元素不允许更改
                cell.deleteButton.isHidden = true
                cell.isUserInteractionEnabled = false
                cell.domainLabel.alpha = 0.5
            }else {
                cell.isUserInteractionEnabled = true
                cell.domainLabel.alpha = 1
                cell.deleteButton.isHidden = self.isEdit
            }
        }else {
            cell.isUserInteractionEnabled = true
            cell.domainLabel.alpha = 1
            cell.domainLabel.text = (self.domains![indexPath.row] as! DomainItemObject).name
            cell.deleteButton.isHidden = true
        }
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let resubaleView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "domainSectionHeader", for: indexPath) as! DomainCollectionReusableView
        if indexPath.section == 0 {
            resubaleView.editButton.isHidden = false
            resubaleView.sectionHeaderTitleLabel.text = "我关注的学科"
            resubaleView.delegate = self
            if self.isEdit {
                resubaleView.editButton.setTitle("编辑", for: .normal)
            }else {
                resubaleView.editButton.setTitle("完成", for: .normal)
            }
        }else {
            resubaleView.editButton.isHidden = true
            resubaleView.sectionHeaderTitleLabel.text = "点击添加学科关注"
        }
        return resubaleView
    }
}
extension DomainViewController: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if !self.isEdit {
            print("选择了\(indexPath.section)行\(indexPath.item)列")
            if indexPath.section == 1 {
                let selectedDomianItem = self.domains![indexPath.item] as! DomainItemObject
                self.selectedDomains.add(selectedDomianItem)
                self.domains?.removeObject(at: indexPath.item)
                self.collectionView.reloadData()
            }
            
        }
    }
}
extension DomainViewController: DomainCollectionCellDelegate {
    func deleteSelectedDomain(cell: DomainCollectionCell) {
        let indexPath = self.collectionView.indexPath(for: cell)!
        let selectedDomianItem = self.selectedDomains[indexPath.item] as! DomainItemObject
        self.domains!.insert(selectedDomianItem, at: 0)
        self.selectedDomains.removeObject(at: indexPath.item)
        self.collectionView.reloadData()
    }
}
extension DomainViewController: DomainCollectionReusableViewDelegate {
    func editAction() {
        self.isEdit = !self.isEdit
    }
}
class DomainCollectionCell: UICollectionViewCell {
    @IBOutlet weak var domainLabel: UILabel!
    @IBOutlet weak var deleteButton: UIButton!
    var delegate: DomainCollectionCellDelegate?
    @IBAction func touchAction(_ sender: UIButton) {
        if self.delegate != nil {
            self.delegate?.deleteSelectedDomain(cell: self)
        }
    }
}
protocol DomainCollectionCellDelegate {
    func deleteSelectedDomain(cell: DomainCollectionCell) -> Void
}
class DomainCollectionReusableView: UICollectionReusableView {
    @IBOutlet weak var sectionHeaderTitleLabel: UILabel!
    @IBOutlet weak var editButton: UIButton!
    var delegate: DomainCollectionReusableViewDelegate?
    @IBAction func touchAction(_ sender: UIButton) {
        if (self.delegate != nil) {
            self.delegate?.editAction()
        }
    }
}
protocol DomainCollectionReusableViewDelegate {
    func editAction() -> Void
}
